package com.jt9003.service.impl;


import com.jt9003.dao.TemplateMapper;
import com.jt9003.pojo.*;
import com.jt9003.service.TemplateService;
import com.jt9003.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

//模板管理Service实现类
@Service
public class TemplateServiceimpl implements TemplateService {

    @Resource
    private TemplateMapper templateMappe;
    @Resource
    private RedisUtil redisUtil;

    //添加各种模板  患者病例模板
    @Override
    public int addTemplate(Template template) {
        //获取Redis中挂号单的key，将全部Key删除
        Set<String> Keys=redisUtil.keys("midical:TemplateController:*");
        for (String key:Keys){
            redisUtil.del(key);
        }
        return templateMappe.addTemplate(template);
    }

    //获取病例模板的数据
    @Override
    public List<Template> getTemplate() {
        Object object = redisUtil.get("midical:TemplateController:getTemplate:");
        if (object!=null){
            return (List<Template>)object;
        }else{
            List<Template> list = templateMappe.getTemplate();
            redisUtil.set("midical:TemplateController:getTemplate:",list,3600);
            return list;
        }
        //return templateMappe.getTemplate();
    }

    //查询所有状态是可用的药品
    @Override
    public List<Drugs> getDrugs() {
        Object object = redisUtil.get("midical:TemplateController:getDrugs:");
        if (object!=null){
            return (List<Drugs>)object;
        }else{
            List<Drugs> list = templateMappe.getDrugs();
            redisUtil.set("midical:TemplateController:getDrugs:",list,120);
            return list;
        }
        //return templateMappe.getDrugs();
    }

    //查询状态=15的用法
    @Override
    public List<Usage> selectUsage() {
        Object object = redisUtil.get("midical:TemplateController:selectUsage:");
        if (object!=null){
            return (List<Usage>)object;
        }else{
            List<Usage> list = templateMappe.selectUsage();
            redisUtil.set("midical:TemplateController:selectUsage:",list,120);
            return list;
        }
      //  return templateMappe.selectUsage();
    }

    //药品与模板对应表插入数据
    @Override
    public int addTemplate_drugs(List<Template_drugs> list) {
        return templateMappe.addTemplate_drugs(list);
    }

    //查询院内协调方数据
    @Override
    public List<Template> getAgreementPartyTemplate() {
        Object object = redisUtil.get("midical:TemplateController:getAgreementPartyTemplate:");
        if (object!=null){
            return (List<Template>)object;
        }else{
            List<Template> list = templateMappe.getAgreementPartyTemplate();
            redisUtil.set("midical:TemplateController:getAgreementPartyTemplate:",list,3600);
            return list;
        }
    }

    //根据id删除模板和药品模板对应表  删除院内协调方和调理方
    @Override
    public int delTemplate(int id) {
        //获取Redis中院内协调方和调理方的key，将全部Key删除
        Set<String> Keys=redisUtil.keys("midical:TemplateController:getAgreementPartyTemplate:*");
        Set<String> Keys2=redisUtil.keys("midical:TemplateController:getOrganizeTemplate:*");
        for (String key:Keys){
            redisUtil.del(key);
        }
        for (String key:Keys2){
            redisUtil.del(key);
        }
        return templateMappe.delTemplate(id);
    }

    //根据id删除患者病例模板
    @Override
    public int delTemplate2(int id) {
        Set<String> Keys=redisUtil.keys("midical:TemplateController:getTemplate:*");
        for (String key:Keys){
            redisUtil.del(key);
        }
        return templateMappe.delTemplate2(id);
    }

    //查询调理方数据
    @Override
    public List<Template> getOrganizeTemplate() {
        Object object = redisUtil.get("midical:TemplateController:getOrganizeTemplate:");
        if (object!=null){
            return (List<Template>)object;
        }else{
            List<Template> list = templateMappe.getOrganizeTemplate();
            redisUtil.set("midical:TemplateController:getOrganizeTemplate:",list,3600);
            return list;
        }
    }

    //查询调理方数据总数
    @Override
    public int OrganizeTemplateTotal() {
        return templateMappe.OrganizeTemplateTotal();
    }

    //查询所有医嘱模板
    @Override
    public List<Template> getMedical_advice() {
        Object object = redisUtil.get("midical:TemplateController:getMedical_advice:");
        if (object!=null){
            return (List<Template>)object;
        }else{
            List<Template> list = templateMappe.getMedical_advice();
            redisUtil.set("midical:TemplateController:getMedical_advice:",list,3600);
            return list;
        }
    }
}
